package com.mqb.handler.netty;

import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.charset.StandardCharsets;

public class ServerHandlerInitializer extends ChannelInitializer<SocketChannel> {

    private static final Logger log = LoggerFactory.getLogger(ServerHandlerInitializer.class);


    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();
        pipeline.addLast(new LengthFieldBasedFrameDecoder(65535, 0, 4, 0, 4));
        pipeline.addLast(new StringDecoder());
        pipeline.addLast(new LoginMsgInboundHandler());


        pipeline.addLast(new RequestMsgInboundHandler());

//        pipeline.addLast(new ChannelOutboundHandlerAdapter() {
//            @Override
//            public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
//                ByteBuf buf = (ByteBuf) msg;
//                log.info("写出内容{}到{}", buf.toString(StandardCharsets.UTF_8), ctx.channel());
//                super.write(ctx, msg, promise);
//            }
//        });
        pipeline.addLast(new StringEncoder());
    }
}
